/**
 * A simple caps lock detection plugin for jQuery. Inspired by the "24 ways:
 * Capturing Caps Lock" article by Stuart Landridge
 * (http://24ways.org/2007/capturing-caps-lock).
 *
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2008 Michael J. I. Jackson
 * @license     http://www.opensource.org/licenses/mit-license.php
 * @version     1.0.0
 */

/**
 * Sets up a keypress listener on the selected elements that can be used to
 * handle situations where the caps lock may be activated (for example, on a
 * password field). The callback will be called in the scope of the current
 * element with one parameter - a boolean indicating whether or not the caps
 * lock is on.
 *
 * @param   Function    cb      The callback function to call
 * @return  jQuery              The jQuery object
 * @public
 */
jQuery.fn.caps = function(cb){
    return this.keypress(function(e){
        var w = e.which ? e.which : (e.keyCode ? e.keyCode : -1);
        var s = e.shiftKey ? e.shiftKey : (e.modifiers ? !!(e.modifiers & 4) : false);
        var c = ((w >= 65 && w <= 90) && !s) || ((w >= 97 && w <= 122) && s);
        cb.call(this, c);
    });
};
